﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace App.Untiys
{
    /// <summary>
    /// 事件处理类
    /// </summary>
    [Description("事件处理类")]
    public sealed class EventHelper
    {
        /// <summary>
        /// 异步派发有参事件
        /// </summary>
        /// <typeparam name="TEventArgs"></typeparam>
        /// <param name="sender"></param>
        /// <param name="handle"></param>
        /// <param name="e">事件参数</param>
        public static void DispatchEventAsync<TEventArgs>(object sender, EventHandler<TEventArgs> handle, TEventArgs e)
            where TEventArgs : EventArgs
        {
            if (handle != null)
            {
                Delegate[] dlgList = handle.GetInvocationList();
                if (dlgList != null)
                {
                    Hashtable _hasJudge = new Hashtable();
                    foreach (Delegate dlg in dlgList)
                    {
                        if (!_hasJudge.ContainsKey(dlg))
                        {
                            ((EventHandler<TEventArgs>)dlg).BeginInvoke(sender, e, null, null);
                            _hasJudge.Add(dlg, "");
                        }
                    }
                    _hasJudge.Clear();
                    _hasJudge = null;
                    GC.Collect();
                }
            }
        }
    }
}
